import type {
  AxiosRequestConfig,
  AxiosResponse,
  AxiosInstance,
  InternalAxiosRequestConfig,
} from "axios"
import { Promise } from "es6-promise"

export interface BaseRequestConfig extends AxiosRequestConfig {
  containHeader?: Boolean
  hasNotClearSameRequest?: Boolean
}

export interface RequestInterceptors {
  requestInterceptors?: (
    config: InternalAxiosRequestConfig
  ) => InternalAxiosRequestConfig
  requestInterceptorsCatch?: (err: any) => any

  responseInterceptors?: <T = AxiosResponse>(config: T) => T
  responseInterceptorsCatch?: (err: any) => any
}

export interface RequestConfig extends BaseRequestConfig {
  interceptors?: RequestInterceptors
}

export interface CancelRequestSource {
  [index: string]: () => void
}

type CustomRequired<T, K extends keyof T> = {
  [P in K]-?: T[P]
} & Omit<T, K>

export type IUrlUniKey = CustomRequired<
  Pick<AxiosRequestConfig, "method" | "url" | "params" | "data">,
  "url"
>

declare class Request {
  instance: AxiosInstance
  interceptorsOj?: RequestInterceptors
  cancelRequestSourceList?: CancelRequestSource[]
  requestUrlList?: string[]
  constructor(config: RequestConfig)
  private delUrl
  private urlUniqKey
  private filterUrls
  private excelCancelRequests
  private removeRepeatRequest
  request<T>(config: RequestConfig): Promise<T>
  cancelAllRequest(): void
  cancelRequest(args: IUrlUniKey | IUrlUniKey[]): void
}
export default Request
